package com.mathlearning.auth.config;

import com.mathlearning.auth.interceptor.AdminInterceptor;
import com.mathlearning.auth.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private AuthInterceptor authInterceptor;

    @Autowired
    private AdminInterceptor adminInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 管理员权限拦截器（优先级最高）
        registry.addInterceptor(adminInterceptor)
                .addPathPatterns(
                        "/api/admin/**",              // 管理员接口
                        "/api/learning/admin/**"      // 在线学习管理接口
                )
                .order(1);

        // 普通用户认证拦截器
        registry.addInterceptor(authInterceptor)
                .addPathPatterns(
                        "/api/user/**",           // 用户相关接口
                        "/api/discussions/**",    // 讨论区接口
                        "/api/comments/**",       // 评论接口
                        "/api/messages/**",       // 私信接口
                        "/api/problems/**",       // 题库接口
                        "/api/learning/**"        // 在线学习接口
                )
                .excludePathPatterns(
                        "/api/auth/**",           // 认证相关接口不需要拦截
                        "/api/home/**",           // 首页接口不需要拦截
                        "/api/admin/**",          // 管理员接口已由AdminInterceptor处理
                        "/api/learning/admin/**", // 学习管理接口已由AdminInterceptor处理
                        "/uploads/**"             // 静态资源不需要拦截
                )
                .order(2);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置静态资源访问 - 头像等上传文件
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:uploads/")
                .setCachePeriod(3600);
        
        // 其他静态资源
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(3600);
    }
}



